/*
 * UserForm2.java
 *
 * Created on February 8, 2008, 6:08 PM
 */

package com.floreantpos.ui.forms;

import java.util.List;

import javax.swing.DefaultComboBoxModel;

import com.floreantpos.PosException;
import com.floreantpos.model.Member;

import com.floreantpos.model.UserType;
import com.floreantpos.model.dao.MemberDAO;

import com.floreantpos.model.util.IllegalModelStateException;
import com.floreantpos.ui.BeanEditor;
import com.floreantpos.ui.dialog.POSMessageDialog;
import com.floreantpos.ui.views.MemberView;
import com.floreantpos.util.POSUtil;
import java.awt.GridBagConstraints;


/**
 * 
 * @author rodaya
 */
public class MemberForm extends BeanEditor {
        private MemberView memberView;
        private Integer newID;    
	/** Creates new form UserForm2 */
	public MemberForm() {
            memberView = new MemberView();
            GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
		add(memberView, gridBagConstraints);
		//UserTypeDAO dao = new UserTypeDAO();
		//List<UserType> userTypes = dao.findAll();

		//cbUserType.setModel(new DefaultComboBoxModel(userTypes.toArray()));
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated
	// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
	
		
	// Variables declaration - do not modify                     
	

	// End of variables declaration                   
	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public String getDisplayText() {
		if (isEditMode())
			return "Edit User";

		return "Add new user";
	}

	private boolean editMode;
        public boolean isEditMode() {
		return editMode;
	}

	public void setEditMode(boolean editMode) {
		this.editMode = editMode;
		
	}
	@Override
	public boolean save() {
		updateModel();

		Member user = (Member) getBean();
		MemberDAO userDAO = MemberDAO.getInstance();

		if (!editMode) {
			Member user2 = userDAO.findMemberByDL(user.getDlNum());
			if (user2 != null) {
				POSMessageDialog.showError(this, "Patient with DL Number: " + user.getDlNum() + " already exists.");
				return false;
			}
		}

		try {
                        //userDAO.save(user);
			userDAO.saveOrUpdate(user, editMode);
		} catch (PosException x) {
			POSMessageDialog.showError(this, x.getMessage(), x);
			x.printStackTrace();
			return false;
		} catch (Exception x) {
			POSMessageDialog.showError(this, "Could not save user", x);
			x.printStackTrace();
			return false;
		}

		return true;
	}
        //built from UserForm
	@Override
	protected boolean updateModel() {
		Member user = null;
		if (!(getBean() instanceof Member)) {
			user = new Member();
		}
		else {
			user = (Member) getBean();
		}

		setBean(memberView.updateMember());

		//setBean(user);
		return true;
	}

	@Override
	protected void updateView() {
		if (!(getBean() instanceof Member)) {
			return;
		}
		Member user = (Member) getBean();
		memberView.setMember(user);
	}
        public void setId(Integer id) {
		if (id != null) {
                    this.newID = id;
                }
        }

	
	
}
